package com.springboot.springboot4.Servlet;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

/**
 * @ClassName:
 * @author: CKC
 * @DateTime: 2021/1/30 14:41
 *
 * 1.MyServlet -->  /my
 * 2.DispatcherServlet -->  /
 *
 * 方法二
 *
 * @Configuration(proxyBeanMethods = true):保证依赖的组件始终是单实例的   细节
 */
//@Configuration(proxyBeanMethods = true)
public class MyRegistConfig {

    @Bean
    public ServletRegistrationBean myServlet(){
        
        MyServlet myServlet = new MyServlet();
        return new ServletRegistrationBean(myServlet,"/my","/my02");
    }
    
    @Bean
    public FilterRegistrationBean myFilter(){
        
        MyFilter myFilter1 = new MyFilter();
        //第一种写法
//        return new FilterRegistrationBean(myFilter1,myServlet());
//        这里myServlet()是什么路径就 过滤什么路径
        
        //写法二
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(myFilter1);
        
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/my","/css/*"));
        
        return filterRegistrationBean;
    }
    
    @Bean
    public ServletListenerRegistrationBean myListener(){
        
        return new ServletListenerRegistrationBean(new MyServletContextListener());
    }
}